#ifndef SYSTEMTRAYMANAGER_H
#define SYSTEMTRAYMANAGER_H

#include <QObject>
#include <QSystemTrayIcon>
#include <QMenu>
#include <QAction>
#include <QIcon>

class BackendService;

class SystemTrayManager : public QObject
{
    Q_OBJECT
    Q_PROPERTY(bool trayVisible READ isTrayVisible NOTIFY trayVisibleChanged)

public:
    explicit SystemTrayManager(QObject *parent = nullptr);
    ~SystemTrayManager();

    // 显示/隐藏托盘图标
    void showTrayIcon();
    void hideTrayIcon();
    void setTrayVisible(bool visible);
    bool isTrayVisible() const;

    // 设置托盘提示文本
    void setTrayToolTip(const QString &tooltip);

    // 设置 BackendService（用于检查登录状态）
    void setBackendService(BackendService *backendService);

    // 更新菜单（根据登录状态）
    void updateMenu(bool isLoggedIn);

public slots:
    // 托盘图标激活处理
    void onTrayIconActivated(QSystemTrayIcon::ActivationReason reason);

    // 菜单项触发处理
    void onShowActionTriggered();
    void onLoginActionTriggered();
    void onQuitActionTriggered();
    void onLogoutActionTriggered();

signals:
    // 托盘图标可见性变化
    void trayVisibleChanged(bool visible);

    // 托盘图标激活
    void trayIconActivated(QSystemTrayIcon::ActivationReason reason);

    // 显示窗口请求
    void showWindowRequested();

    // 退出请求
    void quitRequested();

    // 登录请求
    void loginRequested();

    // 注销请求
    void logoutRequested();

private:
    // 创建托盘图标
    void createTrayIcon();

    // 创建托盘菜单
    void createTrayMenu();

private:
    QSystemTrayIcon *m_trayIcon;
    QMenu *m_trayMenu;
    QAction *m_showAction;
    QAction *m_loginAction;
    QAction *m_logoutAction;
    QAction *m_quitAction;
    QIcon m_defaultIcon;
    BackendService *m_backendService;
};

#endif // SYSTEMTRAYMANAGER_H

